<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2017-11-24 18:40:04
 */
if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class McategoryContrl
{
    public function index()
    {
        global $_G;
        $categorys = C::t('#jameson_read#jamesonread_categorys')->fetch_all_cate();
        include template('jameson_read:mcategory_index');
    }
    public function post()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            foreach ($_GET['delcate'] as $key => $cate) {
                C::t('#jameson_read#jamesonread_categorys')->delete($key);
                unset($_GET['name'][$key]);
            }
            /*增加子类*/
            if (isset($_GET['newsubcate']) && is_array($_GET['newsubcate'])) {
                foreach ($_GET['newsubcate'] as $key => $value) {
                    if (is_array($value) && !empty($value)) {
                        /*只针对填写了分类名字的input*/
                        foreach ($value as $key2 => $value2) {
                            $value2 && C::t('#jameson_read#jamesonread_categorys')->insert(array(
                                'category_name' => daddslashes(dhtmlspecialchars($value2)),
                                'desco'         => daddslashes($_GET['newsubdesco'][$key][$key2]),
                                'ordernum'      => intval($_GET['newsuborder'][$key][$key2]),
                                'parent_id'     => $key,
                                'is_top'        => '0',
                                'is_display'    => '1',
                                'addtime'       => TIMESTAMP));
                        }
                    }
                }
            }
            /*增加父类*/
            if (isset($_GET['newroot']) && is_array($_GET['newroot'])) {
                foreach ($_GET['newroot'] as $key => $value) {
                    if (trim($value)) {
                        /*只针对填写了分类名字的input*/
                        C::t('#jameson_read#jamesonread_categorys')->insert(array(
                            'category_name' => daddslashes(dhtmlspecialchars($value)),
                            'desco'         => daddslashes($_GET['newrootdesco'][$key]),
                            'ordernum'      => intval($_GET['newrootorder'][$key]),
                            'parent_id'     => '0',
                            'is_top'        => '0',
                            'is_display'    => '1',
                            'addtime'       => TIMESTAMP)
                        );
                    }
                }
            }
            /*点击提交时更新所有*/
            if (is_array($_GET['name']) && !empty($_GET['name'])) {
                foreach ($_GET['name'] as $key => $value) {
                    $category_id   = intval($key);
                    $category_name = daddslashes(dhtmlspecialchars($value));
                    $desco         = daddslashes(dhtmlspecialchars($_GET['desco'][$key]));
                    $ordernum      = intval($_GET['order'][$key]);
                    C::t('#jameson_read#jamesonread_categorys')->update($key, array(
                        'category_name' => $category_name,
                        'ordernum'      => $ordernum,
                        'desco'         => $desco)
                    );
                }
            }
            C::t('#jameson_read#jamesonread_topics')->clear_cache(array('zhuanti'));
            C::t('#jameson_read#jamesonread_books')->clear_cache(array('jamesonreadnewtuijian','jamesonreadnewpaihang'));
            C::t('#jameson_read#jamesonread_categorys')->clear_cache(array('fetch_all_cate','allparent-0'));
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mcategory&act=index', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mcategory&act=index', 'error');
        }
    }
    // 删除
    public function del()
    {
        global $_G;
        if (FORMHASH == trim($_GET['formhash']) && ($category_id = intval($_GET['category_id']))) {
            C::t('#jameson_read#jamesonread_categorys')->delete($category_id);
            C::t('#jameson_read#jamesonread_topics')->clear_cache(array('zhuanti'));
            C::t('#jameson_read#jamesonread_categorys')->clear_cache(array('fetch_all_cate','allparent-0'));
            cpmsg(lang('plugin/jameson_read', 'shanchuchenggong'), dreferer(), 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'shanchushibai'), dreferer(), 'error');
        }
    }
}